package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.dto.AppTrainDTO;
import com.whfc.emp.dto.AppTrainEmpDTO;
import com.whfc.emp.param.*;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsTrainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 人员管理-培训
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年8月30日
 */
@RestController
@RequestMapping("/ms/api/train")
public class AppTrainController extends BaseController {
    @Autowired
    private MsTrainService msTrainService;

    /**
     * 人员管理-培训列表
     *
     * @param user
     * @param request
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated AppTrainListParam request,
                       @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                       @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        request.setStartTime(startDate);
        request.setEndTime(endDate);
        logger.info("人员管理-培训列表，loginer:{},AppTrainListParam:{}", user.getUsername(), request.toString());
        PageData<AppTrainDTO> pageData = msTrainService.list(request);
        return ResultUtil.success(pageData);
    }

    /**
     * 人员管理-培训列表
     *
     * @param user
     * @param empId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/empList")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam(value = "empId") Integer empId,
                       @RequestParam(value = "pageNum") Integer pageNum,
                       @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("人员管理-培训列表（人员id），loginer:{},empId:{},pageNum:{},pageSize:{}"
                , user.getUsername(), empId, pageNum, pageSize);
        PageData<AppTrainEmpDTO> pageData = msTrainService.list(empId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }


    /**
     * 人员管理- 新增培训
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/train/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainAddParam request) {
        logger.info("人员管理-增加培训，loginer:{},AppTrainAddParam:{}", user.getUsername(), request.toString());
        msTrainService.add(request);
        return ResultUtil.success();
    }


    /**
     * 人员管理-编辑培训
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/train/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainEditParam request) {
        logger.info("人员管理-编辑培训，loginer:{},AppTrainEditParam:{}", user.getUsername(), request.toString());
        msTrainService.edit(request);
        return ResultUtil.success();
    }


    /**
     * 人员管理-删除培训
     *
     * @param user
     * @param trainId
     * @return
     */
    @RequiresPermissions("emp/train/del")
    @PostMapping("/del/{trainId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("trainId") Integer trainId) {
        logger.info("人员管理-删除培训,loginer:{},trianId:{}", user.getUsername(), trainId);
        msTrainService.del(trainId);
        return ResultUtil.success();

    }


    /**
     * 人员管理-完成培训
     *
     * @param user
     * @param trainId
     * @return
     */
    @RequiresPermissions("emp/train/complete")
    @PostMapping("/complete/{trainId}")
    public Result complete(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("trainId") Integer trainId) {
        logger.info("人员管理-完成培训,loginer:{},trianId:{}", user.getUsername(), trainId);
        msTrainService.complete(trainId);
        return ResultUtil.success();
    }


    /**
     * 人员管理-培训人员列表
     *
     * @param user
     * @param request
     * @return
     */
    @GetMapping("/emp/list")
    public Result trainEmpList(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated AppTrainEmpListParam request) {
        logger.info("人员管理-培训人员列表,loginer:{},AppTrainEmpListParam:{}", user.getUsername(), request.toString());
        PageData<AppTrainEmpDTO> pageData = msTrainService.trainEmpList(request);
        return ResultUtil.success(pageData);
    }


    /**
     * 人员管理-添加培训猿人
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/train/addEmps")
    @PostMapping("/emp/add")
    public Result trainEmpAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainEmpAddParam request) {
        logger.info("人员管理-添加培训人员，loginer:{},AppTrainEmpAddParam:{}", user.getUsername(), request.toString());
        msTrainService.trainEmpAdd(request);
        return ResultUtil.success();
    }


    /**
     * 人员管理-删除培训猿人
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/train/delEmp")
    @PostMapping("/emp/del")
    public Result trainEmpDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainEmpDelParam request) {
        logger.info("人员管理-删除培训人员，loginer:{},AppTrainEmpDelParam:{}", user.getUsername(), request.toString());
        msTrainService.trainEmpDel(request);
        return ResultUtil.success();
    }


    /**
     * 提交修改成绩
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/train/addScore")
    @PostMapping("/emp/score")
    public Result score(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainScoreParam request) {
        logger.info("提交修改成绩,loginer:{},AppTrainScoreParam:{}", user.getUsername(), request.toString());
        msTrainService.score(request);
        return ResultUtil.success();
    }


    /**
     * 获取可添加的培训人员列表
     *
     * @param user
     * @param pageSize
     * @param pageNum
     * @param trainId
     * @return
     */
    @GetMapping("/getEmpList")
    public Result getEmpList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("trainId") Integer trainId,
                             @RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam(value = "groupId", required = false) Integer groupId) {
        logger.info("获取可添加的培训人员列表,loginer:{},pageNum:{},pageSize:{},trainId:{},keyword:{},groupId:{}",
                user.getUsername(), pageNum, pageSize, trainId, keyword, groupId);
        PageData<AppEmpDTO> pageData = msTrainService.getEmpList(pageNum, pageSize, trainId, keyword, groupId);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/uploadImg")
    public Result uploadImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam(value = "file", required = false) MultipartFile file) {
        logger.info("人员培训-上传图片,loginer:{}", user.getUsername());
        OssPathDTO path = msTrainService.uploadImg(file);
        return ResultUtil.success(path);
    }

    @PostMapping("/emp/img")
    public Result empImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam(value = "trainEmpId") Integer trainEmpId,
                         @RequestParam(value = "imgUrlList") List<String> imgUrlList) {
        logger.info("人员培训-人员图片,loginer:{}, trainEmpId:{}, imgUrlList:{}", user.getUsername(), trainEmpId, imgUrlList);
        msTrainService.saveTrainEmpImg(trainEmpId, imgUrlList);
        return ResultUtil.success();
    }

    @PostMapping("/anon/upload/sgin")
    @Deprecated
    public Result uploadSignImg(@RequestParam("signImg") String signImg,
                                @RequestParam("trainEmpId") Integer trainEmpId) {
        logger.info("人员培训-上传图片,trainEmpId:{}", trainEmpId);
        msTrainService.uploadSignImg(signImg, trainEmpId);
        return ResultUtil.success();
    }
}
